// productList.js
const regeneratorRuntime = require('../../lib/runtime');
const wx = require('../../lib/wx');
const Api = require('../../utils/api.js');
const wxapp = require('../../lib/wxapp');

// 获取应用实例
const app = getApp();
Page({
  data: {
    filterActive: '综合',
    sortStatus: '',
    class_id : '',
    brandDetail: {
      logo: '',
      name: '',
      description: '',
      list:[
        {
          imgUrl: '../../images/products/product_img1.png',
          url: '../productDetail/productDetail',
          brandName: '',
          description: '',
          current_price: '',
          orign_price: '',
          shop: '',
          people: 0,
          joined: 0,
        },
        
      ],
    },
    actionActive: false,
    animationData: {},
    windowHeight: 0,
    filterDetail: {
      isOnly: false,
      type: [
        {
          title: '款式',
          isMultiple: true,
          value: ['男款', '女款', '儿童',],
        },
        {
          title: '分类',
          isMultiple: true,
          value: ['包包', '服装', '鞋履', '配饰', '家居',],
        },
        {
          title: '价格区间',
          isMultiple: false,
          value: ['500元以下', '500元 - 1000元', '1000元 - 2000元', '2000元 - 5000元', '5000元以上',],
        }
        // ,
        // {
        //   title: '来源',
        //   isMultiple: true,
        //   value: ['BNKR', 'East Dane', 'Farfetch', 'FeelUnique', 'FWRD', 'Harrods', 'Kll ArtStore'],
        // },
      ],
    },
  },

  goToDetail: wxapp.handler(function* goToDetail(next, e){
    const url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    });

    yield next;
  }),

  goToMenu: wxapp.handler(function* goToMenu(next, e){
    const url = e.currentTarget.dataset.url;
    wx.reLaunch({
      url,
    });

    yield next;
  }),

  switchFilter: wxapp.handler(function* switchFilter(next, e) {
    const activeFilter = e.currentTarget.dataset.filter;
    const pageData = this.data;
    let sortStatus = pageData.sortStatus;
    let screening = 0;
    if(activeFilter === '筛选'){
      this.actionShow();
      sortStatus = '';
      return;
    } else if (activeFilter === '价格') {
      if(sortStatus === ''){
        sortStatus = 'up';
      } else if(sortStatus === 'up'){
        sortStatus = 'down';
      } else if(sortStatus === 'down'){
        sortStatus = 'up';
      }
    } else{
      screening = 1;
      sortStatus = '';
    }
    this.setData({
      sortStatus,
      filterActive: activeFilter,
    });

    var that = this;
    var class_id = that.data.class_id;
    var getGoodsList = Api.getGoodsList();
    // 商品列表
    wx.request({
      url: getGoodsList,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {
        //发送给后台的数据
        class_sub_id: class_id,
        price_order:sortStatus,
        screening:screening

      },
      complete: function (res) {
        console.log(res.data.data);
        that.setData({ brandDetail: res.data.data });
      }
    });

    yield next;
  }),

  // action弹出
  actionShow(){
    const pageData = this.data;
    const animation = wx.createAnimation({
      duration: 200,
    });
    this.animation = animation;

    if (pageData.actionActive === true) {
      animation.right('-642rpx').step();
      this.setData({
        actionActive: false,
      });
    } else {
      animation.right('0').step();
      this.setData({
        actionActive: true,
      });
    }

    this.setData({
      animationData: animation.export(),
    });
  },

  closeAction: wxapp.handler(function* closeAction(next){
    this.animation.right('-642rpx').step();

    this.setData({
      animationData: this.animation.export(),
      actionActive: false,
    });

    yield next;
  }),

  checkboxChange: wxapp.handler(function* checkboxChange(next, e){
    const pageData = this.data;
    let filterDetail = pageData.filterDetail;
    if(filterDetail.isOnly){
      filterDetail.isOnly = false;
    } else{
      filterDetail.isOnly = true;
    }
    this.setData({
      filterDetail,
    });

    yield next;
  }),

  checkType: wxapp.handler(function* checkType(next, e){
    let activeType = e.target.dataset.value;
    if(this.data.activeType === activeType){
      activeType = '';
    }
    this.setData({
      activeType,
    });
    


    yield next;
  }),

  goodsSearch: wxapp.handler(function* goodsSearch(next, e) {
    let activeType = this.data.activeType;
    var that = this;
    var getGoodsList = Api.getGoodsList(); 
    var class_id = this.data.class_id;

    this.animation.right('-642rpx').step();

    this.setData({
      animationData: this.animation.export(),
      actionActive: false,
    });
console.log(1);
    // 商品列表
    wx.request({
      url: getGoodsList,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {//发送给后台的数据
        class_sub_id: class_id,
        activeType: activeType
      },
      complete: function (res) {
        that.setData({ brandDetail: res.data.data });
      }
    });
    console.log(2);


    yield next;
  }),

  onShow: wxapp.handler(function* onShow(next) {

    yield next;
  }),

  onLoad: wxapp.handler(function* onLoad(next,e) {
    const systemInfo = wx.getSystemInfoSync();
    const windowHeight = systemInfo.windowHeight;
    const class_id = e.class_id;
    this.setData({
      windowHeight,
      class_id:class_id
    });

   

    var that = this;
    var getGoodsList = Api.getGoodsList();
    // 商品列表
    wx.request({
      url: getGoodsList,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {//发送给后台的数据
        class_sub_id: class_id
      },
      complete: function (res) {
        that.setData({ brandDetail: res.data.data });
      }
    });
// console.log(1111);
    var getGoodsSearchType = Api.getGoodsSearchType();
    // console.log(getGoodsSearchType);
    // 商品列表
    wx.request({
      url: getGoodsSearchType,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {//发送给后台的数据
        id: class_id
      },
      complete: function (res) {
        that.setData({ filterDetail: res.data.data });
      }
    });

    // console.log(this.filterDetail);
    yield next;
  }),

});
